<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Chapter 3 Sequence objects &mdash; Biopython_en 1.0 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Biopython_en 1.0 documentation" href="index.html" />
    <link rel="next" title="Chapter 4 Sequence annotation objects" href="chr4.html" />
    <link rel="prev" title="Chapter 2 Quick Start – What can you do with Biopython?" href="chr2.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="chr4.html" title="Chapter 4 Sequence annotation objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="chr2.html" title="Chapter 2 Quick Start – What can you do with Biopython?"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Biopython_en 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="chapter-3-sequence-objects">
<h1>Chapter 3  Sequence objects<a class="headerlink" href="#chapter-3-sequence-objects" title="Permalink to this headline">¶</a></h1>
<p>Biological sequences are arguably the central object in Bioinformatics,
and in this chapter we’ll introduce the Biopython mechanism for dealing
with sequences, the <tt class="docutils literal"><span class="pre">Seq</span></tt> object. Chapter <a class="reference external" href="#chapter:SeqRecord">4</a>
will introduce the related <tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object, which combines the
sequence information with any annotation, used again in
Chapter <a class="reference external" href="#chapter:Bio.SeqIO">5</a> for Sequence Input/Output.</p>
<p>Sequences are essentially strings of letters like <tt class="docutils literal"><span class="pre">AGTACACTGGT</span></tt>, which
seems very natural since this is the most common way that sequences are
seen in biological file formats.</p>
<p>There are two important differences between <tt class="docutils literal"><span class="pre">Seq</span></tt> objects and standard
Python strings. First of all, they have different methods. Although the
<tt class="docutils literal"><span class="pre">Seq</span></tt> object supports many of the same methods as a plain string, its
<tt class="docutils literal"><span class="pre">translate()</span></tt> method differs by doing biological translation, and
there are also additional biologically relevant methods like
<tt class="docutils literal"><span class="pre">reverse_complement()</span></tt>. Secondly, the <tt class="docutils literal"><span class="pre">Seq</span></tt> object has an important
attribute, <tt class="docutils literal"><span class="pre">alphabet</span></tt>, which is an object describing what the
individual characters making up the sequence string “mean”, and how they
should be interpreted. For example, is <tt class="docutils literal"><span class="pre">AGTACACTGGT</span></tt> a DNA sequence,
or just a protein sequence that happens to be rich in Alanines,
Glycines, Cysteines and Threonines?</p>
<div class="section" id="sequences-and-alphabets">
<h2>3.1  Sequences and Alphabets<a class="headerlink" href="#sequences-and-alphabets" title="Permalink to this headline">¶</a></h2>
<p>The alphabet object is perhaps the important thing that makes the
<tt class="docutils literal"><span class="pre">Seq</span></tt> object more than just a string. The currently available
alphabets for Biopython are defined in the <tt class="docutils literal"><span class="pre">Bio.Alphabet</span></tt> module.
We’ll use the IUPAC alphabets
(<tt class="docutils literal"><span class="pre">`http://www.chem.qmw.ac.uk/iupac/</span></tt> &lt;<a class="reference external" href="http://www.chem.qmw.ac.uk/iupac/">http://www.chem.qmw.ac.uk/iupac/</a>&gt;`__)
here to deal with some of our favorite objects: DNA, RNA and Proteins.</p>
<p><tt class="docutils literal"><span class="pre">Bio.Alphabet.IUPAC</span></tt> provides basic definitions for proteins, DNA and
RNA, but additionally provides the ability to extend and customize the
basic definitions. For instance, for proteins, there is a basic
IUPACProtein class, but there is an additional ExtendedIUPACProtein
class providing for the additional elements “U” (or “Sec” for
selenocysteine) and “O” (or “Pyl” for pyrrolysine), plus the ambiguous
symbols “B” (or “Asx” for asparagine or aspartic acid), “Z” (or “Glx”
for glutamine or glutamic acid), “J” (or “Xle” for leucine isoleucine)
and “X” (or “Xxx” for an unknown amino acid). For DNA you’ve got choices
of IUPACUnambiguousDNA, which provides for just the basic letters,
IUPACAmbiguousDNA (which provides for ambiguity letters for every
possible situation) and ExtendedIUPACDNA, which allows letters for
modified bases. Similarly, RNA can be represented by IUPACAmbiguousRNA
or IUPACUnambiguousRNA.</p>
<p>The advantages of having an alphabet class are two fold. First, this
gives an idea of the type of information the Seq object contains.
Secondly, this provides a means of constraining the information, as a
means of type checking.</p>
<p>Now that we know what we are dealing with, let’s look at how to utilize
this class to do interesting work. You can create an ambiguous sequence
with the default generic alphabet like this:</p>
<p>However, where possible you should specify the alphabet explicitly when
creating your sequence objects - in this case an unambiguous DNA
alphabet object:</p>
<p>Unless of course, this really is an amino acid sequence:</p>
</div>
<div class="section" id="sequences-act-like-strings">
<h2>3.2  Sequences act like strings<a class="headerlink" href="#sequences-act-like-strings" title="Permalink to this headline">¶</a></h2>
<p>In many ways, we can deal with Seq objects as if they were normal Python
strings, for example getting the length, or iterating over the elements:</p>
<p>You can access elements of the sequence in the same way as for strings
(but remember, Python counts from zero!):</p>
<p>The <tt class="docutils literal"><span class="pre">Seq</span></tt> object has a <tt class="docutils literal"><span class="pre">.count()</span></tt> method, just like a string. Note
that this means that like a Python string, this gives a
<em>non-overlapping</em> count:</p>
<p>For some biological uses, you may actually want an overlapping count
(i.e. 3 in this trivial example). When searching for single letters,
this makes no difference:</p>
<p>While you could use the above snippet of code to calculate a GC%, note
that the <tt class="docutils literal"><span class="pre">Bio.SeqUtils</span></tt> module has several GC functions already built.
For example:</p>
<p>Note that using the <tt class="docutils literal"><span class="pre">Bio.SeqUtils.GC()</span></tt> function should automatically
cope with mixed case sequences and the ambiguous nucleotide S which
means G or C.</p>
<p>Also note that just like a normal Python string, the <tt class="docutils literal"><span class="pre">Seq</span></tt> object is
in some ways “read-only”. If you need to edit your sequence, for example
simulating a point mutation, look at the
Section <a class="reference external" href="#sec:mutable-seq">3.12</a> below which talks about the
<tt class="docutils literal"><span class="pre">MutableSeq</span></tt> object.</p>
</div>
<div class="section" id="slicing-a-sequence">
<h2>3.3  Slicing a sequence<a class="headerlink" href="#slicing-a-sequence" title="Permalink to this headline">¶</a></h2>
<p>A more complicated example, let’s get a slice of the sequence:</p>
<p>Two things are interesting to note. First, this follows the normal
conventions for Python strings. So the first element of the sequence is
0 (which is normal for computer science, but not so normal for biology).
When you do a slice the first item is included (i.e. 4 in this case) and
the last is excluded (12 in this case), which is the way things work in
Python, but of course not necessarily the way everyone in the world
would expect. The main goal is to stay consistent with what Python does.</p>
<p>The second thing to notice is that the slice is performed on the
sequence data string, but the new object produced is another <tt class="docutils literal"><span class="pre">Seq</span></tt>
object which retains the alphabet information from the original <tt class="docutils literal"><span class="pre">Seq</span></tt>
object.</p>
<p>Also like a Python string, you can do slices with a start, stop and
<em>stride</em> (the step size, which defaults to one). For example, we can get
the first, second and third codon positions of this DNA sequence:</p>
<p>Another stride trick you might have seen with a Python string is the use
of a -1 stride to reverse the string. You can do this with a <tt class="docutils literal"><span class="pre">Seq</span></tt>
object too:</p>
</div>
<div class="section" id="turning-seq-objects-into-strings">
<h2>3.4  Turning Seq objects into strings<a class="headerlink" href="#turning-seq-objects-into-strings" title="Permalink to this headline">¶</a></h2>
<p>If you really do just need a plain string, for example to write to a
file, or insert into a database, then this is very easy to get:</p>
<p>Since calling <tt class="docutils literal"><span class="pre">str()</span></tt> on a <tt class="docutils literal"><span class="pre">Seq</span></tt> object returns the full sequence as
a string, you often don’t actually have to do this conversion
explicitly. Python does this automatically with a print statement:</p>
<p>You can also use the <tt class="docutils literal"><span class="pre">Seq</span></tt> object directly with a <tt class="docutils literal"><span class="pre">%s</span></tt> placeholder
when using the Python string formatting or interpolation operator
(<tt class="docutils literal"><span class="pre">%</span></tt>):</p>
<p>This line of code constructs a simple FASTA format record (without
worrying about line wrapping). Section <a class="reference external" href="#sec:SeqRecord-format">4.5</a>
describes a neat way to get a FASTA formatted string from a
<tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object, while the more general topic of reading and
writing FASTA format sequence files is covered in
Chapter <a class="reference external" href="#chapter:Bio.SeqIO">5</a>.</p>
<p><em>NOTE:</em> If you are using Biopython 1.44 or older, using <tt class="docutils literal"><span class="pre">str(my_seq)</span></tt>
will give just a truncated representation. Instead use
<tt class="docutils literal"><span class="pre">my_seq.tostring()</span></tt> (which is still available in the current Biopython
releases for backwards compatibility):</p>
</div>
<div class="section" id="concatenating-or-adding-sequences">
<h2>3.5  Concatenating or adding sequences<a class="headerlink" href="#concatenating-or-adding-sequences" title="Permalink to this headline">¶</a></h2>
<p>Naturally, you can in principle add any two Seq objects together - just
like you can with Python strings to concatenate them. However, you can’t
add sequences with incompatible alphabets, such as a protein sequence
and a DNA sequence:</p>
<p>If you <em>really</em> wanted to do this, you’d have to first give both
sequences generic alphabets:</p>
<p>Here is an example of adding a generic nucleotide sequence to an
unambiguous IUPAC DNA sequence, resulting in an ambiguous nucleotide
sequence:</p>
</div>
<div class="section" id="changing-case">
<h2>3.6  Changing case<a class="headerlink" href="#changing-case" title="Permalink to this headline">¶</a></h2>
<p>Python strings have very useful <tt class="docutils literal"><span class="pre">upper</span></tt> and <tt class="docutils literal"><span class="pre">lower</span></tt> methods for
changing the case. As of Biopython 1.53, the <tt class="docutils literal"><span class="pre">Seq</span></tt> object gained
similar methods which are alphabet aware. For example,</p>
<p>These are useful for doing case insensitive matching:</p>
<p>Note that strictly speaking the IUPAC alphabets are for upper case
sequences only, thus:</p>
</div>
<div class="section" id="nucleotide-sequences-and-reverse-complements">
<h2>3.7  Nucleotide sequences and (reverse) complements<a class="headerlink" href="#nucleotide-sequences-and-reverse-complements" title="Permalink to this headline">¶</a></h2>
<p>For nucleotide sequences, you can easily obtain the complement or
reverse complement of a <tt class="docutils literal"><span class="pre">Seq</span></tt> object using its built-in methods:</p>
<p>As mentioned earlier, an easy way to just reverse a <tt class="docutils literal"><span class="pre">Seq</span></tt> object (or a
Python string) is slice it with -1 step:</p>
<p>In all of these operations, the alphabet property is maintained. This is
very useful in case you accidentally end up trying to do something weird
like take the (reverse)complement of a protein sequence:</p>
<p>The example in Section <a class="reference external" href="#sec:SeqIO-reverse-complement">5.5.3</a>
combines the <tt class="docutils literal"><span class="pre">Seq</span></tt> object’s reverse complement method with
<tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> for sequence input/output.</p>
</div>
<div class="section" id="transcription">
<h2>3.8  Transcription<a class="headerlink" href="#transcription" title="Permalink to this headline">¶</a></h2>
<p>Before talking about transcription, I want to try and clarify the strand
issue. Consider the following (made up) stretch of double stranded DNA
which encodes a short peptide:</p>
<p>The actual biological transcription process works from the template
strand, doing a reverse complement (TCAG → CUGA) to give the mRNA.
However, in Biopython and bioinformatics in general, we typically work
directly with the coding strand because this means we can get the mRNA
sequence just by switching T → U.</p>
<p>Now let’s actually get down to doing a transcription in Biopython.
First, let’s create <tt class="docutils literal"><span class="pre">Seq</span></tt> objects for the coding and template DNA
strands:</p>
<p>These should match the figure above - remember by convention nucleotide
sequences are normally read from the 5’ to 3’ direction, while in the
figure the template strand is shown reversed.</p>
<p>Now let’s transcribe the coding strand into the corresponding mRNA,
using the <tt class="docutils literal"><span class="pre">Seq</span></tt> object’s built in <tt class="docutils literal"><span class="pre">transcribe</span></tt> method:</p>
<p>As you can see, all this does is switch T → U, and adjust the alphabet.</p>
<p>If you do want to do a true biological transcription starting with the
template strand, then this becomes a two-step process:</p>
<p>The <tt class="docutils literal"><span class="pre">Seq</span></tt> object also includes a back-transcription method for going
from the mRNA to the coding strand of the DNA. Again, this is a simple U
→ T substitution and associated change of alphabet:</p>
<p><em>Note:</em> The <tt class="docutils literal"><span class="pre">Seq</span></tt> object’s <tt class="docutils literal"><span class="pre">transcribe</span></tt> and <tt class="docutils literal"><span class="pre">back_transcribe</span></tt>
methods were added in Biopython 1.49. For older releases you would have
to use the <tt class="docutils literal"><span class="pre">Bio.Seq</span></tt> module’s functions instead, see
Section <a class="reference external" href="#sec:seq-module-functions">3.14</a>.</p>
</div>
<div class="section" id="translation">
<h2>3.9  Translation<a class="headerlink" href="#translation" title="Permalink to this headline">¶</a></h2>
<p>Sticking with the same example discussed in the transcription section
above, now let’s translate this mRNA into the corresponding protein
sequence - again taking advantage of one of the <tt class="docutils literal"><span class="pre">Seq</span></tt> object’s
biological methods:</p>
<p>You can also translate directly from the coding strand DNA sequence:</p>
<p>You should notice in the above protein sequences that in addition to the
end stop character, there is an internal stop as well. This was a
deliberate choice of example, as it gives an excuse to talk about some
optional arguments, including different translation tables (Genetic
Codes).</p>
<p>The translation tables available in Biopython are based on those <a class="reference external" href="http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi">from
the NCBI</a>
(see the next section of this tutorial). By default, translation will
use the <em>standard</em> genetic code (NCBI table id 1). Suppose we are
dealing with a mitochondrial sequence. We need to tell the translation
function to use the relevant genetic code instead:</p>
<p>You can also specify the table using the NCBI table number which is
shorter, and often included in the feature annotation of GenBank files:</p>
<p>Now, you may want to translate the nucleotides up to the first in frame
stop codon, and then stop (as happens in nature):</p>
<p>Notice that when you use the <tt class="docutils literal"><span class="pre">to_stop</span></tt> argument, the stop codon itself
is not translated - and the stop symbol is not included at the end of
your protein sequence.</p>
<p>You can even specify the stop symbol if you don’t like the default
asterisk:</p>
<p>Now, suppose you have a complete coding sequence CDS, which is to say a
nucleotide sequence (e.g. mRNA – after any splicing) which is a whole
number of codons (i.e. the length is a multiple of three), commences
with a start codon, ends with a stop codon, and has no internal in-frame
stop codons. In general, given a complete CDS, the default translate
method will do what you want (perhaps with the <tt class="docutils literal"><span class="pre">to_stop</span></tt> option).
However, what if your sequence uses a non-standard start codon? This
happens a lot in bacteria – for example the gene yaaX in <tt class="docutils literal"><span class="pre">E.</span> <span class="pre">coli</span></tt>
K12:</p>
<p>In the bacterial genetic code <tt class="docutils literal"><span class="pre">GTG</span></tt> is a valid start codon, and while
it does <em>normally</em> encode Valine, if used as a start codon it should be
translated as methionine. This happens if you tell Biopython your
sequence is a complete CDS:</p>
<p>In addition to telling Biopython to translate an alternative start codon
as methionine, using this option also makes sure your sequence really is
a valid CDS (you’ll get an exception if not).</p>
<p>The example in Section <a class="reference external" href="#sec:SeqIO-translate">18.1.3</a> combines the
<tt class="docutils literal"><span class="pre">Seq</span></tt> object’s translate method with <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> for sequence
input/output.</p>
</div>
<div class="section" id="translation-tables">
<h2>3.10  Translation Tables<a class="headerlink" href="#translation-tables" title="Permalink to this headline">¶</a></h2>
<p>In the previous sections we talked about the <tt class="docutils literal"><span class="pre">Seq</span></tt> object translation
method (and mentioned the equivalent function in the <tt class="docutils literal"><span class="pre">Bio.Seq</span></tt> module
– see Section <a class="reference external" href="#sec:seq-module-functions">3.14</a>). Internally these
use codon table objects derived from the NCBI information at
<tt class="docutils literal"><span class="pre">`ftp://ftp.ncbi.nlm.nih.gov/entrez/misc/data/gc.prt</span></tt> &lt;<a class="reference external" href="ftp://ftp.ncbi.nlm.nih.gov/entrez/misc/data/gc.prt">ftp://ftp.ncbi.nlm.nih.gov/entrez/misc/data/gc.prt</a>&gt;`__,
also shown on
<tt class="docutils literal"><span class="pre">`http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi</span></tt> &lt;<a class="reference external" href="http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi">http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi</a>&gt;`__
in a much more readable layout.</p>
<p>As before, let’s just focus on two choices: the Standard translation
table, and the translation table for Vertebrate Mitochondrial DNA.</p>
<p>Alternatively, these tables are labeled with ID numbers 1 and 2,
respectively:</p>
<p>You can compare the actual tables visually by printing them:</p>
<p>and:</p>
<p>You may find these following properties useful – for example if you are
trying to do your own gene finding:</p>
</div>
<div class="section" id="comparing-seq-objects">
<h2>3.11  Comparing Seq objects<a class="headerlink" href="#comparing-seq-objects" title="Permalink to this headline">¶</a></h2>
<p>Sequence comparison is actually a very complicated topic, and there is
no easy way to decide if two sequences are equal. The basic problem is
the meaning of the letters in a sequence are context dependent - the
letter “A” could be part of a DNA, RNA or protein sequence. Biopython
uses alphabet objects as part of each <tt class="docutils literal"><span class="pre">Seq</span></tt> object to try and capture
this information - so comparing two <tt class="docutils literal"><span class="pre">Seq</span></tt> objects means considering
both the sequence strings <em>and</em> the alphabets.</p>
<p>For example, you might argue that the two DNA <tt class="docutils literal"><span class="pre">Seq</span></tt> objects
<tt class="docutils literal"><span class="pre">Seq(&quot;ACGT&quot;,</span> <span class="pre">IUPAC.unambiguous_dna)</span></tt> and
<tt class="docutils literal"><span class="pre">Seq(&quot;ACGT&quot;,</span> <span class="pre">IUPAC.ambiguous_dna)</span></tt> should be equal, even though they
do have different alphabets. Depending on the context this could be
important.</p>
<p>This gets worse – suppose you think
<tt class="docutils literal"><span class="pre">Seq(&quot;ACGT&quot;,</span> <span class="pre">IUPAC.unambiguous_dna)</span></tt> and <tt class="docutils literal"><span class="pre">Seq(&quot;ACGT&quot;)</span></tt> (i.e. the
default generic alphabet) should be equal. Then, logically,
<tt class="docutils literal"><span class="pre">Seq(&quot;ACGT&quot;,</span> <span class="pre">IUPAC.protein)</span></tt> and <tt class="docutils literal"><span class="pre">Seq(&quot;ACGT&quot;)</span></tt> should also be equal.
Now, in logic if <em>A</em>=<em>B</em> and <em>B</em>=<em>C</em>, by transitivity we expect
<em>A</em>=<em>C</em>. So for logical consistency we’d require
<tt class="docutils literal"><span class="pre">Seq(&quot;ACGT&quot;,</span> <span class="pre">IUPAC.unambiguous_dna)</span></tt> and
<tt class="docutils literal"><span class="pre">Seq(&quot;ACGT&quot;,</span> <span class="pre">IUPAC.protein)</span></tt> to be equal – which most people would
agree is just not right. This transitivity problem would also have
implications for using <tt class="docutils literal"><span class="pre">Seq</span></tt> objects as Python dictionary keys.</p>
<p>So, what does Biopython do? Well, the equality test is the default for
Python objects – it tests to see if they are the same object in memory.
This is a very strict test:</p>
<p>If you actually want to do this, you can be more explicit by using the
Python <tt class="docutils literal"><span class="pre">id</span></tt> function,</p>
<p>Now, in every day use, your sequences will probably all have the same
alphabet, or at least all be the same type of sequence (all DNA, all
RNA, or all protein). What you probably want is to just compare the
sequences as strings – so do this explicitly:</p>
<p>As an extension to this, while you can use a Python dictionary with
<tt class="docutils literal"><span class="pre">Seq</span></tt> objects as keys, it is generally more useful to use the sequence
a string for the key. See also Section <a class="reference external" href="#sec:seq-to-string">3.4</a>.</p>
</div>
<div class="section" id="mutableseq-objects">
<h2>3.12  MutableSeq objects<a class="headerlink" href="#mutableseq-objects" title="Permalink to this headline">¶</a></h2>
<p>Just like the normal Python string, the <tt class="docutils literal"><span class="pre">Seq</span></tt> object is “read only”,
or in Python terminology, immutable. Apart from wanting the <tt class="docutils literal"><span class="pre">Seq</span></tt>
object to act like a string, this is also a useful default since in many
biological applications you want to ensure you are not changing your
sequence data:</p>
<p>Observe what happens if you try to edit the sequence:</p>
<p>However, you can convert it into a mutable sequence (a <tt class="docutils literal"><span class="pre">MutableSeq</span></tt>
object) and do pretty much anything you want with it:</p>
<p>Alternatively, you can create a <tt class="docutils literal"><span class="pre">MutableSeq</span></tt> object directly from a
string:</p>
<p>Either way will give you a sequence object which can be changed:</p>
<p>Do note that unlike the <tt class="docutils literal"><span class="pre">Seq</span></tt> object, the <tt class="docutils literal"><span class="pre">MutableSeq</span></tt> object’s
methods like <tt class="docutils literal"><span class="pre">reverse_complement()</span></tt> and <tt class="docutils literal"><span class="pre">reverse()</span></tt> act in-situ!</p>
<p>An important technical difference between mutable and immutable objects
in Python means that you can’t use a <tt class="docutils literal"><span class="pre">MutableSeq</span></tt> object as a
dictionary key, but you can use a Python string or a <tt class="docutils literal"><span class="pre">Seq</span></tt> object in
this way.</p>
<p>Once you have finished editing your a <tt class="docutils literal"><span class="pre">MutableSeq</span></tt> object, it’s easy
to get back to a read-only <tt class="docutils literal"><span class="pre">Seq</span></tt> object should you need to:</p>
<p>You can also get a string from a <tt class="docutils literal"><span class="pre">MutableSeq</span></tt> object just like from a
<tt class="docutils literal"><span class="pre">Seq</span></tt> object (Section <a class="reference external" href="#sec:seq-to-string">3.4</a>).</p>
</div>
<div class="section" id="unknownseq-objects">
<h2>3.13  UnknownSeq objects<a class="headerlink" href="#unknownseq-objects" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">UnknownSeq</span></tt> object is a subclass of the basic <tt class="docutils literal"><span class="pre">Seq</span></tt> object and
its purpose is to represent a sequence where we know the length, but not
the actual letters making it up. You could of course use a normal
<tt class="docutils literal"><span class="pre">Seq</span></tt> object in this situation, but it wastes rather a lot of memory
to hold a string of a million “N” characters when you could just store a
single letter “N” and the desired length as an integer.</p>
<p>You can of course specify an alphabet, meaning for nucleotide sequences
the letter defaults to “N” and for proteins “X”, rather than just “?”.</p>
<p>You can use all the usual <tt class="docutils literal"><span class="pre">Seq</span></tt> object methods too, note these give
back memory saving <tt class="docutils literal"><span class="pre">UnknownSeq</span></tt> objects where appropriate as you might
expect:</p>
<p>You may be able to find a use for the <tt class="docutils literal"><span class="pre">UnknownSeq</span></tt> object in your own
code, but it is more likely that you will first come across them in a
<tt class="docutils literal"><span class="pre">SeqRecord</span></tt> object created by <tt class="docutils literal"><span class="pre">Bio.SeqIO</span></tt> (see
Chapter <a class="reference external" href="#chapter:Bio.SeqIO">5</a>). Some sequence file formats don’t
always include the actual sequence, for example GenBank and EMBL files
may include a list of features but for the sequence just present the
contig information. Alternatively, the QUAL files used in sequencing
work hold quality scores but they <em>never</em> contain a sequence – instead
there is a partner FASTA file which <em>does</em> have the sequence.</p>
</div>
<div class="section" id="working-with-directly-strings">
<h2>3.14  Working with directly strings<a class="headerlink" href="#working-with-directly-strings" title="Permalink to this headline">¶</a></h2>
<p>To close this chapter, for those you who <em>really</em> don’t want to use the
sequence objects (or who prefer a functional programming style to an
object orientated one), there are module level functions in <tt class="docutils literal"><span class="pre">Bio.Seq</span></tt>
will accept plain Python strings, <tt class="docutils literal"><span class="pre">Seq</span></tt> objects (including
<tt class="docutils literal"><span class="pre">UnknownSeq</span></tt> objects) or <tt class="docutils literal"><span class="pre">MutableSeq</span></tt> objects:</p>
<p>You are, however, encouraged to work with <tt class="docutils literal"><span class="pre">Seq</span></tt> objects by default.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Chapter 3  Sequence objects</a><ul>
<li><a class="reference internal" href="#sequences-and-alphabets">3.1  Sequences and Alphabets</a></li>
<li><a class="reference internal" href="#sequences-act-like-strings">3.2  Sequences act like strings</a></li>
<li><a class="reference internal" href="#slicing-a-sequence">3.3  Slicing a sequence</a></li>
<li><a class="reference internal" href="#turning-seq-objects-into-strings">3.4  Turning Seq objects into strings</a></li>
<li><a class="reference internal" href="#concatenating-or-adding-sequences">3.5  Concatenating or adding sequences</a></li>
<li><a class="reference internal" href="#changing-case">3.6  Changing case</a></li>
<li><a class="reference internal" href="#nucleotide-sequences-and-reverse-complements">3.7  Nucleotide sequences and (reverse) complements</a></li>
<li><a class="reference internal" href="#transcription">3.8  Transcription</a></li>
<li><a class="reference internal" href="#translation">3.9  Translation</a></li>
<li><a class="reference internal" href="#translation-tables">3.10  Translation Tables</a></li>
<li><a class="reference internal" href="#comparing-seq-objects">3.11  Comparing Seq objects</a></li>
<li><a class="reference internal" href="#mutableseq-objects">3.12  MutableSeq objects</a></li>
<li><a class="reference internal" href="#unknownseq-objects">3.13  UnknownSeq objects</a></li>
<li><a class="reference internal" href="#working-with-directly-strings">3.14  Working with directly strings</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="chr2.html"
                        title="previous chapter">Chapter 2  Quick Start – What can you do with Biopython?</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="chr4.html"
                        title="next chapter">Chapter 4  Sequence annotation objects</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/chr3.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="chr4.html" title="Chapter 4 Sequence annotation objects"
             >next</a> |</li>
        <li class="right" >
          <a href="chr2.html" title="Chapter 2 Quick Start – What can you do with Biopython?"
             >previous</a> |</li>
        <li><a href="index.html">Biopython_en 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2013, Biopython.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
    </div>
  </body>
</html>